package cn.iocoder.yudao.module.shop.service.handlestrategyuser;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.shop.dal.dataobject.handlestrategy.ShopHandleStrategyDO;
import cn.iocoder.yudao.module.shop.dal.mysql.handlestrategy.ShopHandleStrategyMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.handlestrategyuser.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.handlestrategyuser.ShopHandleStrategyUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.handlestrategyuser.ShopHandleStrategyUserConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.handlestrategyuser.ShopHandleStrategyUserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 艾灸师绑定的工资策略 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopHandleStrategyUserServiceImpl implements ShopHandleStrategyUserService {

    @Resource
    private ShopHandleStrategyUserMapper handleStrategyUserMapper;

    @Resource
    private ShopHandleStrategyMapper handleStrategyMapper;

    @Override
    public Long createHandleStrategyUser(ShopHandleStrategyUserCreateReqVO createReqVO) {
        // 插入
        ShopHandleStrategyUserDO handleStrategyUser = ShopHandleStrategyUserConvert.INSTANCE.convert(createReqVO);

        ShopHandleStrategyUserDO exitedHandleStrategyUser = getHandleStrategeByUserId(handleStrategyUser.getUserId());
        if(exitedHandleStrategyUser != null){
            throw exception(HANDLE_STRATEGY_USER_HAD_EXISTS);
        }

        handleStrategyUserMapper.insert(handleStrategyUser);
        // 返回
        return handleStrategyUser.getId();
    }

    @Override
    public void updateHandleStrategyUser(ShopHandleStrategyUserUpdateReqVO updateReqVO) {
        // 校验存在
        validateHandleStrategyUserExists(updateReqVO.getId());
        // 更新
        ShopHandleStrategyUserDO updateObj = ShopHandleStrategyUserConvert.INSTANCE.convert(updateReqVO);
        handleStrategyUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteHandleStrategyUser(Long id) {
        // 校验存在
        validateHandleStrategyUserExists(id);
        // 删除
        handleStrategyUserMapper.deleteById(id);
    }

    private void validateHandleStrategyUserExists(Long id) {
        if (handleStrategyUserMapper.selectById(id) == null) {
            throw exception(HANDLE_STRATEGY_USER_NOT_EXISTS);
        }
    }

    @Override
    public ShopHandleStrategyUserDO getHandleStrategyUser(Long id) {
        return handleStrategyUserMapper.selectById(id);
    }

    @Override
    public List<ShopHandleStrategyUserDO> getHandleStrategyUserList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return handleStrategyUserMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopHandleStrategyUserDO> getHandleStrategyUserPage(ShopHandleStrategyUserPageReqVO pageReqVO) {
        return handleStrategyUserMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopHandleStrategyUserDO> getHandleStrategyUserList(ShopHandleStrategyUserExportReqVO exportReqVO) {
        return handleStrategyUserMapper.selectList(exportReqVO);
    }

    @Override
    public ShopHandleStrategyUserDO getHandleStrategeByUserId(Long userId){
        return handleStrategyUserMapper.selectOne(ShopHandleStrategyUserDO::getUserId, userId);
    }

    @Override
    public List<ShopHandleStrategyUserRespVO> getHandleStrategeByUserIds(Collection<Long> userIds){
        if(userIds.size() == 0){
            return new ArrayList<>();
        }

        List<ShopHandleStrategyUserDO> strategyUserDOList = handleStrategyUserMapper.selectList(ShopHandleStrategyUserDO::getUserId, userIds);

        Set<Long> strategyIds = CollectionUtils.convertSet(strategyUserDOList, ShopHandleStrategyUserDO::getStrategyId);

        List<ShopHandleStrategyDO> strategyDOList = handleStrategyMapper.selectList(ShopHandleStrategyDO::getId, strategyIds);

        return ShopHandleStrategyUserConvert.INSTANCE.convertList(strategyUserDOList, strategyDOList);
    }

}
